package fei.yuzhi.com.loveflowers.ui.ProductActivity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;

import java.util.ArrayList;
import java.util.List;

import fei.yuzhi.com.loveflowers.R;
import fei.yuzhi.com.loveflowers.bean.ProductBean;
import fei.yuzhi.com.loveflowers.fragment.firstpagefragment.detail.DetailActivity;
import fei.yuzhi.com.loveflowers.ui.ShaixuanActivity.ShaiXuanActivity;

/**
 * Created by Administrator on 2016/10/9.
 */
public class ProductActivity extends AppCompatActivity implements ProductContract.ProductView {
    private ProductPresenter presenter;
    private ListView lv_product;
    private List<ProductBean.DatasBean.GoodsListBean> list1,list2,list3;
    private RadioButton rb_producttuijian,rb_productprice,rb_productsale;
    private Button tv_productshaixuan;
    private ProductAdapter adapter;
    private int page=1;
    private boolean changed=true;
    private ImageView iv_tabproduct;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product);
        lv_product= (ListView) findViewById(R.id.lv_product);
        rb_producttuijian= (RadioButton) findViewById(R.id.rb_producttuijian);
        rb_productprice= (RadioButton) findViewById(R.id.rb_productprice);
        rb_productsale= (RadioButton) findViewById(R.id.rb_productsale);
        iv_tabproduct= (ImageView) findViewById(R.id.iv_tbproduct);
        tv_productshaixuan= (Button) findViewById(R.id.tv_productshaixuan);
        Intent intent=getIntent();
        String keyword=intent.getStringExtra("keyword");
        presenter=new ProductPresenter(page,keyword,this);
        presenter.ProductConnected();
        presenter.ProductPriceConnected();
        presenter.ProductSaleConnected();
        tv_productshaixuan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(ProductActivity.this, ShaiXuanActivity.class);
                startActivity(intent);
                overridePendingTransition(R.anim.anim_shaixuan_out,R.anim.anim_shaixuan_out);
            }
        });
    }


    @Override
    public void onGetProductBeanSuccess(List<ProductBean.DatasBean.GoodsListBean> list) {
        list1=new ArrayList<>();
        list1=list;
        adapter=new ProductAdapter(list,this);
        lv_product.setAdapter(adapter);
        lv_product.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(ProductActivity.this, DetailActivity.class);
                intent.putExtra("data",list1.get(position).getGoodsId());
                startActivity(intent);
            }
        });
    }

    @Override
    public void onGetProductBeanFail() {

    }

    @Override
    public void onGetProductPriceSuccess(List<ProductBean.DatasBean.GoodsListBean> list) {
        list2=new ArrayList<>();
        list2=list;

    }

    @Override
    public void onGetProductPriceFail() {
    }

    @Override
    public void onGetProductSaleSuccess(List<ProductBean.DatasBean.GoodsListBean> list) {
        list3=new ArrayList<>();
        list3=list;
    }

    @Override
    public void onGetProductSaleFail() {

    }



    public void onClick(View v) {

            switch (v.getId()) {
                case R.id.rb_producttuijian:
                    adapter = new ProductAdapter(list1, this);
                    lv_product.setAdapter(adapter);
                    lv_product.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            Intent intent=new Intent(ProductActivity.this, DetailActivity.class);
                            intent.putExtra("data",list1.get(position).getGoodsId());
                            startActivity(intent);
                        }
                    });
                    break;
                case R.id.rb_productprice:
                    adapter = new ProductAdapter(list2, this);
                    lv_product.setAdapter(adapter);
                    lv_product.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            Intent intent=new Intent(ProductActivity.this, DetailActivity.class);
                            intent.putExtra("data",list2.get(position).getGoodsId());
                            startActivity(intent);
                        }
                    });
                    break;
                case R.id.rb_productsale:
                    adapter = new ProductAdapter(list3, this);
                    lv_product.setAdapter(adapter);
                    lv_product.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            Intent intent=new Intent(ProductActivity.this, DetailActivity.class);
                            intent.putExtra("data",list3.get(position).getGoodsId());
                            startActivity(intent);
                        }
                    });
                    break;
                case R.id.iv_product:
                    finish();
                    break;
            }

    }

    public void change(View view){
        if(changed){
            iv_tabproduct.setImageResource(R.mipmap.goods_list_small);
            changed=false;
        }else {
            iv_tabproduct.setImageResource(R.mipmap.goods_list_big);
            changed=true;
        }
    }

}
